package our.pack;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MediaCameraSaveActivity extends Activity{
	
	private Button save,discard;
	private EditText editName;
	private byte[] imageData;
	
	MetaDataDataSource metaDataDatasource;
	PointsDataSource pointsDatasource;
	
	MyPoint point;
	
	private static final String PATH = "/sdcard/DCIM/";
	
	/**
	 *Saves the picture taken in MediaCameraActivity 
	 */
	
	public void onCreate(Bundle SaveInstanceState){
		super.onCreate(SaveInstanceState);
		setContentView(R.layout.media_camera_save);
		
		//db
		metaDataDatasource = new MetaDataDataSource(this);
		metaDataDatasource.open();
		
		//initialize the buttons
		save = (Button) findViewById(R.id.btnSavePhoto);
		discard = (Button) findViewById(R.id.btnDiscardPhoto);
		editName = (EditText) findViewById(R.id.editPhotoName);
		
		//get the point where it's made and get the photo
		Intent i = getIntent();
		Bundle b = i.getExtras();
		imageData = (byte[]) b.get("Photo");
		point = (MyPoint) b.get("Point");
		
		//don't save it when the user clicks discard
		discard.setOnClickListener(new OnClickListener(){
			
			public void onClick(View v){
				finish();
			}
		});
		
		save.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				
				//save it
				StoreByteImage(getApplicationContext(), imageData, 100);
				finish();
			}
		});
	}
	
	//saves it on the sdcard
	public void StoreByteImage(Context mContext, byte[] imageData,int quality) {

        File sdImageMainDirectory = new File(PATH);
		FileOutputStream fileOutputStream = null;
		String nameFile;
		try {

			BitmapFactory.Options options=new BitmapFactory.Options();
			options.inSampleSize = 5;
			
			Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0 ,imageData.length,options);

			fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + editName.getText() + ".jpg");
							
			BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

			myImage.compress(CompressFormat.JPEG, quality, bos);
			
			//set the date
			String[] ids = TimeZone.getAvailableIDs(+1 * 60 * 60 * 1000);					
			SimpleTimeZone cest = new SimpleTimeZone(+1 * 60 * 60 * 1000, ids[0]);
			Calendar cal = new GregorianCalendar(cest);
			String date = cal.get(Calendar.DATE) + "-" + (cal.get(Calendar.MONTH)+1)  + "-" + cal.get(Calendar.YEAR);

			//set it in the database
			MetaData m = metaDataDatasource.createMetaData(editName.getText().toString(), sdImageMainDirectory.toString() + "/" + editName.getText() + ".jpg", date, "photo", point);
			
			bos.flush();
			bos.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
